package com.cblz.bd.server.core.configure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Collections;

/**
 * {@code @Name:} CorsConfig
 * {@code @Description: } 跨域
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2022/11/9 - 周三
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许任何请求
        corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
        // 允许任何请求头
        corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
        // 允许任何方法
        corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
        // 允许任何凭证
        corsConfiguration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsFilter(source);
    }
}
